package sanshi.contect.observer.v8;

import java.util.ArrayList;
import java.util.List;

/**
 * 很多时候，观察者在处理事件得时候，需要事件源对，这个时候就需要将事件元对象放在事件中，作为一个事件的属性。
 *
 * 事件也可以成为一个体系
 */
public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.age = 13;
        child.wakeUp();
    }
}

/**
 * 体系话。T为事件元类类型
 * @param <T>
 */
abstract class Event<T>{
    abstract T getResource();
}

/**
 * 事件类
 */
class WakeUpEvent extends Event<Child>{
    long timeTamp;
    String loc;
    Child resource;


    public WakeUpEvent(long timeTamp, String loc, Child resource){
        this.timeTamp = timeTamp;
        this.loc = loc;
        this.resource = resource;
    }

    @Override
    public String toString() {
        return "WakeUpEvent{" +
                "timeTamp=" + timeTamp +
                ", loc='" + loc + '\'' +
                '}';
    }

    @Override
    Child getResource() {
        return resource;
    }
}

interface Observer{
    void actionOnWakeUp(WakeUpEvent event);
}

/**
 * 观察者
 */
class Dad implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Dad-----》" + event);
        System.out.println("dad feeding ......");
        if (event.resource.age > 12){
            System.out.println("抱个锤子。。。。");
        }
    }
}

class Mum implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Mum-----》" + event);
        System.out.println("mun hugging  ......");
    }
}

class Dog implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Dog-----》" + event);
        System.out.println("wang wang wang ......");
    }
}

/**
 * 被观者
 */
class Child{

    boolean cry;
    public int age;
    List<Observer> observers = new ArrayList<Observer>();

    {
        observers.add(new Dad());
        observers.add(new Mum());
        observers.add(new Dog());
    }

   public boolean isCry(){
        return cry;
   }
   public void wakeUp(){
        cry = true;
        WakeUpEvent event = new WakeUpEvent(System.currentTimeMillis(), "bad", this);
        for (Observer observer : observers){
            observer.actionOnWakeUp(event);
        }
   }
}


